<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/vue/2.6.14/vue.js" type="application/javascript"></script>
</head>
<body>
    <div id="app">
        <!-- 方式2 通过指令 v-on -->
        <img :src="imgPath" ref="imgRef" @error="onError">
    </div>
</body>
<script >
    new Vue({
        el: '#app',
        data() {
            return {
                imgPath: 'http://localhost:8080/undefined.png'
            }
        },
        mounted(){
            // 方式1 通过ref得到dom元素，监听事件
            this.$refs.imgRef.onerror=()=>{
                // this.src='https://img.yzcdn.cn/vant/cat.jpeg'
                this.imgPath='https://img.yzcdn.cn/vant/cat.jpeg'
            }

        },
        methods:{
            onerror(){
                this.imgPath='https://img.yzcdn.cn/vant/cat.jpeg'
            }
        }
    })
</script>
</html>
